package com.nus.parsers;

import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.nus.representations.IvleCodeTableWeekType;
import com.nus.representations.IvleForum;
import com.nus.representations.IvleGradeBook;
import com.nus.representations.IvleLibrary;
import com.nus.representations.IvleModule;
import com.nus.representations.IvleModuleAnnouncements;
import com.nus.representations.IvleNews;
import com.nus.representations.IvleRosters;
import com.nus.representations.IvleTimeTable;
import com.nus.representations.IvleWorkbin;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.nus.representations.IvleNews;
import com.nus.representations.IvleStudentEvents;
import com.nus.representations.IvleTimeTable;
import com.nus.representations.ShuttleMapLocations;

public class ParserAPIs {

	// Parser for Ivle News (we will need a parser method for everything i.e.
	// for module for grade and so on)
	public static ArrayList<IvleNews> parseIvleNews(String jsonString) {
		ArrayList<IvleNews> news = new ArrayList<IvleNews>();
		JSONObject jObject;
		JSONArray jnews = null;
		try {
			jObject = new JSONObject(jsonString);
			jnews = jObject.getJSONArray("Results");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < jnews.length(); i++) {
			try {
				news.add(new IvleNews(jnews.getJSONObject(i).getString(
						"CreatedDate"), jnews.getJSONObject(i).getString(
						"Description"), jnews.getJSONObject(i).getString("ID"),
						jnews.getJSONObject(i).getString("Title")));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return news;

	}

	public static ArrayList<IvleModule> parseIvleModules(String jsonString) {
		ArrayList<IvleModule> module = new ArrayList<IvleModule>();
		JSONObject jObject;
		JSONArray jmodule = null;
		try {
			jObject = new JSONObject(jsonString);
			jmodule = jObject.getJSONArray("Results");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < jmodule.length(); i++) {
			try {
				module.add(new IvleModule(jmodule.getJSONObject(i).getString(
						"CourseCode"),
						jmodule.getJSONObject(i).getString("ID"), jmodule
								.getJSONObject(i).getString("CourseSemester"),
						jmodule.getJSONObject(i).getString("CourseAcadYear")

				));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return module;

	}

	public static ArrayList<IvleModuleAnnouncements> parseIvleModuleAnnouncements(
			String jsonString) {
		ArrayList<IvleModuleAnnouncements> announcement = new ArrayList<IvleModuleAnnouncements>();
		JSONObject jObject;
		JSONArray jannouncement = null;
		JSONObject jcreator = null;
		try {
			jObject = new JSONObject(jsonString);
			jannouncement = jObject.getJSONArray("Results");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (int i = 0; i < jannouncement.length(); i++) {
			try {
				jcreator = jannouncement.getJSONObject(i).getJSONObject(
						"Creator");
				announcement.add(new IvleModuleAnnouncements(jannouncement
						.getJSONObject(i).getString("Title"), jannouncement
						.getJSONObject(i).getString("Description"), jcreator
						.getString("Name"), NdroidAPI.formatDate(jannouncement
						.getJSONObject(i).getString("CreatedDate"))));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		// for(int i=0; i<jannouncement.length(); i++){
		// try {
		// announcement.add(new
		// IvleModuleAnnouncements(jannouncement.getJSONObject(i).getString("Title"),
		// jannouncement.getJSONObject(i).getString("Description"),
		// jannouncement.getJSONObject(i).getString("Name"),jannouncement.getJSONObject(i).getString("CreatedDate")
		// ));
		// } catch (JSONException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		for (int i = 0; i < announcement.size(); i++) {
			Log.i("Parser rohitDescription", announcement.get(i)
					.getDescription());
			Log.i("Parser rohitTitle", announcement.get(i).getTitle());
			// lv_arr[i] = module.get(i).getCourseCode();

		}
		return announcement;
	}

	public static ArrayList<IvleForum> parseIvleForum(String jsonString) {
		ArrayList<IvleForum> forum = new ArrayList<IvleForum>();
		JSONObject jObject, jObject2;
		JSONArray jforum = null, jdataforum = null, jthreads = null, jsubthreads = null;
		try {
			jObject = new JSONObject(jsonString);
			jforum = jObject.getJSONArray("Results");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < jforum.length(); i++) {
			try {
				jdataforum = jforum.getJSONObject(i).getJSONArray("Headings");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		// int j =0;
		for (int i = 0; i < jdataforum.length(); i++) {
			try {
				jthreads = jdataforum.getJSONObject(i).getJSONArray("Threads");
				for (int j = 0; j < jthreads.length(); j++) {
					try {
						JSONObject dummy = jthreads.getJSONObject(j)
								.getJSONObject("Poster");

						forum.add(new IvleForum(jthreads.getJSONObject(j)
								.getString("PostBody"), NdroidAPI
								.formatDate(jthreads.getJSONObject(j)
										.getString("PostDate")), jthreads
								.getJSONObject(j).getString("PostTitle"), dummy
								.getString("Name"), jthreads.getJSONObject(j)
								.getString("isNewPost"), jthreads
								.getJSONObject(j).getString("ID")));
						jsubthreads = jthreads.getJSONObject(j).getJSONArray(
								"Threads");
						for (int k = 0; k < jsubthreads.length(); k++) {
							JSONObject dummy2 = jsubthreads.getJSONObject(k)
									.getJSONObject("Poster");

							forum.add(new IvleForum(jsubthreads
									.getJSONObject(k).getString("PostBody"),
									NdroidAPI.formatDate(jthreads
											.getJSONObject(k).getString(
													"PostDate")), " >"
											+ jsubthreads.getJSONObject(k)
													.getString("PostTitle"),
									dummy2.getString("Name"), jsubthreads
											.getJSONObject(k).getString(
													"isNewPost"), jsubthreads
											.getJSONObject(k).getString("ID")));
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		// for( int j=0;j<forum.size();j++){
		// Log.i(" value","forum array list valueis"+forum.get(j).getBody()+" "+forum.get(j).getDate()+" "+forum.get(j).getTitle());
		//
		// }

		// Log.i(" value of post body is",
		// " post body"+jforum.getJSONObject(i).getString("Results.Headings.Threads.PostBody"));
		return forum;
	}

	public static ArrayList<IvleWorkbin> parseIvleWorkbin(String jsonString) {
		ArrayList<IvleWorkbin> workbin = new ArrayList<IvleWorkbin>();
		JSONObject jObject, jcreator, jassignmentcreator = null;
		JSONArray jworkbin = null, jlecture = null, jlecturefolder = null, jassignmentfolder = null, jassignmentsubfolder = null;
		;
		try {
			jObject = new JSONObject(jsonString);
			jworkbin = jObject.getJSONArray("Results");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < jworkbin.length(); i++) {
			try {
				jlecture = jworkbin.getJSONObject(i).getJSONArray("Folders");
				for (int j = 0; j < jlecture.length(); j++) {

					jlecturefolder = jlecture.getJSONObject(j).getJSONArray(
							"Folders");
					for (int k = 0; k < jlecturefolder.length(); k++) {

						jassignmentsubfolder = jlecturefolder.getJSONObject(k)
								.getJSONArray("Files");
						jcreator = jassignmentsubfolder.getJSONObject(k)
								.getJSONObject("Creator");
						for (int g = 0; g < jassignmentsubfolder.length(); g++) {
							// jassignmentcreator=
							// jassignmentsubfolder.getJSONObject(g).getString("Creator");
							workbin.add(new IvleWorkbin(jcreator
									.getString("Name"), jlecture.getJSONObject(
									j).getString("FolderName"),
									jassignmentsubfolder.getJSONObject(g)
											.getString("FileName"),
									jassignmentsubfolder.getJSONObject(g)
											.getString("FileType"),
									jassignmentsubfolder.getJSONObject(g)
											.getString("FileSize"), NdroidAPI
											.formatDate(jlecture.getJSONObject(
													j).getString("CloseDate")),
									jassignmentsubfolder.getJSONObject(g)
											.getString("ID")));

						}

					}

					jassignmentfolder = jlecture.getJSONObject(j).getJSONArray(
							"Files");
					for (int h = 0; h < jassignmentfolder.length(); h++) {
						jassignmentcreator = jassignmentfolder.getJSONObject(h)
								.getJSONObject("Creator");
						workbin.add(new IvleWorkbin(jassignmentcreator
								.getString("Name"), jlecture.getJSONObject(j)
								.getString("FolderName"), jassignmentfolder
								.getJSONObject(h).getString("FileName"),
								jassignmentfolder.getJSONObject(h).getString(
										"FileType"),
								jassignmentfolder.getJSONObject(h).getString(
										"FileSize"), NdroidAPI
										.formatDate(jlecture.getJSONObject(j)
												.getString("CloseDate")),
								jassignmentfolder.getJSONObject(h).getString(
										"ID")));

					}

				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return workbin;
	}

	public static ArrayList<IvleGradeBook> parseIvleGrade(String jsonString) {
		ArrayList<IvleGradeBook> grade = new ArrayList<IvleGradeBook>();
		JSONObject jObject;
		JSONArray jgradebook = null, jgrade = null;
		try {
			jObject = new JSONObject(jsonString);
			jgradebook = jObject.getJSONArray("Results");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < jgradebook.length(); i++) {
			try {
				// jgrade=jgradebook.getJSONObject(i).getJSONArray("")

				grade.add(new IvleGradeBook(jgradebook.getJSONObject(i)
						.getString("Category"), jgradebook.getJSONObject(i)
						.getString("ItemName"), jgradebook.getJSONObject(i)
						.getString("MarksObtained"), jgradebook
						.getJSONObject(i).getString("MaxMarks")));

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return grade;
	}

	public static ArrayList<IvleRosters> parseIvleRosters(String jsonString) {
		ArrayList<IvleRosters> roster = new ArrayList<IvleRosters>();
		JSONObject jObject;
		JSONArray jroster = null;
		try {
			jObject = new JSONObject(jsonString);
			jroster = jObject.getJSONArray("Results");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < jroster.length(); i++) {
			try {
				// jgrade=jgradebook.getJSONObject(i).getJSONArray("")

				roster.add(new IvleRosters(jroster.getJSONObject(i).getString(
						"Name"), jroster.getJSONObject(i).getString("Email"),
						jroster.getJSONObject(i).getString("UserID")));

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return roster;
	}

	public static ArrayList<IvleLibrary> parseIvleELibrary(String jsonString) {
		ArrayList<IvleLibrary> library = new ArrayList<IvleLibrary>();
		JSONObject jObject;
		JSONArray jlibrary = null, jereserve = null;
		try {
			jObject = new JSONObject(jsonString);
			jlibrary = jObject.getJSONArray("Results");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < jlibrary.length(); i++) {
			try {

				jereserve = jlibrary.getJSONObject(i).getJSONArray("Files");
				for (int j = 0; j < jereserve.length(); j++) {
					library.add(new IvleLibrary(jereserve.getJSONObject(j)
							.getString("Size"), jereserve.getJSONObject(j)
							.getString("Name"), jereserve.getJSONObject(j)
							.getString("Description")));
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return library;
	}

	public static ArrayList<IvleTimeTable> parseIvleTimeTable(String jsonString) {
		ArrayList<IvleTimeTable> timetable = new ArrayList<IvleTimeTable>();
		JSONObject jObject;
		JSONArray jtimetable = null;
		try {
			jObject = new JSONObject(jsonString);
			jtimetable = jObject.getJSONArray("Results");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < jtimetable.length(); i++) {
			try {
				// jgrade=jgradebook.getJSONObject(i).getJSONArray("")

				timetable.add(new IvleTimeTable(jtimetable.getJSONObject(i)
						.getString("ModuleCode"), jtimetable.getJSONObject(i)
						.getString("StartTime"), jtimetable.getJSONObject(i)
						.getString("EndTime"), jtimetable.getJSONObject(i)
						.getString("DayText"), jtimetable.getJSONObject(i)
						.getString("WeekText"), jtimetable.getJSONObject(i)
						.getString("Venue"), jtimetable.getJSONObject(i)
						.getString("LessonType"), jtimetable.getJSONObject(i)
						.getString("WeekCode"), jtimetable.getJSONObject(i)
						.getString("DayCode")));

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return timetable;
	}

	public static ArrayList<IvleStudentEvents> parseIvleStudentEvents(
			String jsonString) {
		ArrayList<IvleStudentEvents> events = new ArrayList<IvleStudentEvents>();
		JSONObject jObject;
		JSONArray jstudevents = null;
		try {
			jObject = new JSONObject(jsonString);
			jstudevents = jObject.getJSONArray("Results");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < jstudevents.length(); i++) {
			try {
				events.add(new IvleStudentEvents(jstudevents.getJSONObject(i)
						.getString("Agenda"), jstudevents.getJSONObject(i)
						.getString("Contact"), jstudevents.getJSONObject(i)
						.getString("Description"), jstudevents.getJSONObject(i)
						.getString("EventDateTime"), jstudevents.getJSONObject(
						i).getString("ID"), jstudevents.getJSONObject(i)
						.getString("Organizer"), jstudevents.getJSONObject(i)
						.getString("Price"), jstudevents.getJSONObject(i)
						.getString("Title"), jstudevents.getJSONObject(i)
						.getString("Venue")));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return events;
	}

	public static ArrayList<ShuttleMapLocations> parseShuttleMapLocations(
			String jsonString) {
		ArrayList<ShuttleMapLocations> smaplocations = new ArrayList<ShuttleMapLocations>();
		JSONObject jObject;
		JSONArray jsmaplocations = null;
		try {
			jObject = new JSONObject(jsonString);
			jsmaplocations = jObject.getJSONArray("BuildingList");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < jsmaplocations.length(); i++) {
			try {
				smaplocations.add(new ShuttleMapLocations(jsmaplocations
						.getJSONObject(i).getString("Title"), jsmaplocations
						.getJSONObject(i).getString("Latitude"), jsmaplocations
						.getJSONObject(i).getString("Longitude")));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return smaplocations;

	}

	public static ArrayList<IvleCodeTableWeekType> IvleWeekCode(
			String jsonString) {
		ArrayList<IvleCodeTableWeekType> weekcode = new ArrayList<IvleCodeTableWeekType>();
		JSONObject jObject;
		JSONArray jweekcode = null;
		try {
			jObject = new JSONObject(jsonString);
			jweekcode = jObject.getJSONArray("Results");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < jweekcode.length(); i++) {
			try {
				weekcode.add(new IvleCodeTableWeekType(jweekcode.getJSONObject(
						i).getString("Description"), jweekcode.getJSONObject(i)
						.getString("WeekCode")));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return weekcode;

	}

}
